package com.szh.cloud.api.classs.controller;

import com.szh.cloud.api.classs.entity.ClassEntity;
import com.szh.cloud.api.classs.entity.SearClassEntity;
import com.szh.cloud.api.classs.service.ApiClassService;
import com.szh.cloud.common.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/class")
public class ApiClassController {

    @Autowired
    private ApiClassService apiClassService;

    @PostMapping("/queryClassListByPage")
    public Result queryClassListByPage(@RequestBody SearClassEntity params) {
        try {
            List<ClassEntity> list = apiClassService.queryClassListByPage(params);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询班级列表失败：" + e.getMessage());
        }
    }

    @PostMapping("/queryClassListTotal")
    public Result queryClassListTotal(@RequestBody SearClassEntity params) {
        try {
            int total = apiClassService.queryClassListTotal(params);
            return Result.success(total);
        } catch (Exception e) {
            return Result.error("获取班级总数失败：" + e.getMessage());
        }
    }

    @PostMapping("/addClass")
    public Result addClass(@RequestBody ClassEntity classEntity) {
        try {
            classEntity.setCreateTime(new Date());
            classEntity.setUpdateTime(new Date());
            classEntity.setIsActive(1);
            apiClassService.addClass(classEntity);
            return Result.success("添加班级成功");
        } catch (Exception e) {
            return Result.error("添加班级失败：" + e.getMessage());
        }
    }

    @PostMapping("/updateClass")
    public Result updateClass(@RequestBody ClassEntity classEntity) {
        try {
            classEntity.setUpdateTime(new Date());
            apiClassService.updateClass(classEntity);
            return Result.success("更新班级成功");
        } catch (Exception e) {
            return Result.error("更新班级失败：" + e.getMessage());
        }
    }

    @PostMapping("/deleteClass")
    public Result deleteClass(@RequestBody List<Integer> ids) {
        try {
            apiClassService.deleteClass(ids);
            return Result.success("删除班级成功");
        } catch (Exception e) {
            return Result.error("删除班级失败：" + e.getMessage());
        }
    }
} 